<?php
/**
 * Created by PhpStorm.
 * User: wugang
 * Date: 2017/11/25
 * Time: 下午5:34
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;


/**
 * App\Models\TaskDaily
 *
 * @property int $id
 * @property int $uid
 * @property string $content 每日任务完成情况留言
 * @property string $img 每日任务完成上传图片证据
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property int|null $state 0: 未确认完成
 * 1: 已确认完成
 * @property int $task_id 任务id
 * @property-read \App\Models\Task $task
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereTaskId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\TaskDaily whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class TaskDaily extends Model
{

    public static function boot()
    {
        //每次新增都向参与者发送一条通知信息, 记录进入redis
        self::created(function (TaskDaily $taskDaily){
            $taskDaily->task->task_join_users->each(function (User $user) use ($taskDaily){
                Cache::getRedis()->lpush('user_notification_'.$user->id,json_encode([
                    'task_id' => $taskDaily->task_id,
                    'task_desc' => $taskDaily->task->desc
                ]));
            });
        });
    }

    protected $table = 'hackathon.task_daily';


    public function task(){
        return $this->belongsTo(\App\Models\Task::class, 'task_id');
    }

}